package com.fyzk.activity.lottery.recommend_order;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fyzk.net.PublicMethod;
import com.tyjc.lottery.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanxs11 on 17/7/4.
 * 我的晒单/跟单Adapter
 */

public class MyRecommendOrderAdapter extends BaseAdapter {

    private Context context;
    private List<MyRecommendOrderBean> infos = new ArrayList<>();
    private int type = 0;

    public MyRecommendOrderAdapter(Context context, List<MyRecommendOrderBean> infos) {
        this.context = context;
        this.infos = infos;
    }

    @Override
    public int getCount() {
        return infos.size();
    }

    @Override
    public Object getItem(int position) {
        return infos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        final MyRecommendOrderBean info = infos.get(position);
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_myrecommendorder, null);
            holder.content_ll = (LinearLayout) convertView.findViewById(R.id.myrr_item_content_ll);
            holder.prizeState_iv = (ImageView) convertView.findViewById(R.id.myrr_item_prizestate_iv);
            holder.time_tv = (TextView) convertView.findViewById(R.id.myrr_item_time);
            holder.endTime_tv = (TextView) convertView.findViewById(R.id.myrr_item_endtime);
            holder.amount_tv = (TextView) convertView.findViewById(R.id.myrr_item_amount);
            holder.nChuan1_tv = (TextView) convertView.findViewById(R.id.myrr_item_n_chuan_1);
            holder.type_tv = (TextView) convertView.findViewById(R.id.myrr_item_type);
            holder.btn_tz = (Button) convertView.findViewById(R.id.btn_tz);
            holder.btn_gd = (Button) convertView.findViewById(R.id.btn_gd);
            holder.ll_tz = (LinearLayout) convertView.findViewById(R.id.hiscenter_tz_content);
            holder.ll_gd = (LinearLayout) convertView.findViewById(R.id.hiscenter_gd_content);
            holder.ll_bt = (LinearLayout) convertView.findViewById(R.id.ll_bt);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (info.getOrderResultStatus() == 3 || info.getOrderResultStatus() == 4) { // 中奖
            holder.prizeState_iv.setVisibility(View.VISIBLE);
            holder.prizeState_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.myrr_prizestate_yes_icon));
        } else if (info.getOrderResultStatus() == 2){   // 未中奖
            holder.prizeState_iv.setVisibility(View.VISIBLE);
            holder.prizeState_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.myrr_prizestate_no_icon));
        } else if (info.getOrderResultStatus() == 6){   // 流单
            holder.prizeState_iv.setVisibility(View.VISIBLE);
            holder.prizeState_iv.setImageDrawable(context.getResources().getDrawable(R.drawable.myrr_prizestate_liu_icon));
        } else {    // 未开奖
            holder.prizeState_iv.setVisibility(View.GONE);
        }

        holder.time_tv.setText(info.getReceiveTime());
        holder.endTime_tv.setText("截止："+info.getEndTime());

        MyRecommendOrderBean.DetailBean detailBean = info.getDetail();
        if (TextUtils.equals("1",info.getIsShowAmt())){
            holder.amount_tv.setText("方案彩金：保密");
        }else {
            holder.amount_tv.setText("方案彩金：" + info.getAmount() / 100 + "米");
        }
        holder.nChuan1_tv.setText("过关方式："+detailBean.getPlayName());

        boolean isEnd = false;  // 比赛是否截止
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  // 2017-07-22 00:00:00
        long endTime = 0;
        try {
            endTime = format.parse(info.getEndTime()).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        isEnd = endTime < System.currentTimeMillis();

//        if (type != 0 && info.getWay() != 0 && !isEnd) {   // 我的晒单、方案保密、比赛未截止
//            holder.type_tv.setVisibility(View.VISIBLE);
//            holder.content_ll.setVisibility(View.GONE);
//        } else {
//            holder.type_tv.setVisibility(View.GONE);
//            holder.content_ll.setVisibility(View.VISIBLE);
//            setContent(holder.content_ll, detailBean.getRaceBetcode());
//        }
        if (info.isShowTz()) {
            holder.ll_gd.setVisibility(View.GONE);
            holder.btn_tz.setTextColor(Color.parseColor("#e91e63"));
            holder.btn_gd.setTextColor(Color.parseColor("#666666"));
            if (type != 0 && info.getWay() != 0 && !isEnd) {   // 方案保密
                holder.type_tv.setVisibility(View.VISIBLE);
                holder.ll_tz.setVisibility(View.GONE);
                holder.ll_bt.setVisibility(View.GONE);
            } else {
                holder.type_tv.setVisibility(View.GONE);
                holder.ll_tz.setVisibility(View.VISIBLE);
                holder.ll_bt.setVisibility(View.VISIBLE);
                setContent(holder.ll_tz, detailBean.getRaceBetcode());
            }
        } else {
            holder.type_tv.setVisibility(View.GONE);
            holder.ll_tz.setVisibility(View.GONE);
            holder.ll_gd.setVisibility(View.VISIBLE);
            holder.ll_bt.setVerticalGravity(View.VISIBLE);
            holder.btn_gd.setTextColor(Color.parseColor("#e91e63"));
            holder.btn_tz.setTextColor(Color.parseColor("#666666"));
        }
        if (info.getGdUser() != null ) {
            setGdUserContent(holder.ll_gd, info.getGdUser());
        }

        holder.btn_tz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                holder.btn_tz.setTextColor(Color.parseColor("#e91e63"));
                holder.btn_gd.setTextColor(Color.parseColor("#666666"));
                holder.ll_gd.setVisibility(View.GONE);
                if (info.getWay() != 0) {   // 方案保密
                    holder.type_tv.setVisibility(View.VISIBLE);
                    holder.ll_tz.setVisibility(View.GONE);
                } else {
                    holder.type_tv.setVisibility(View.GONE);
                    holder.ll_tz.setVisibility(View.VISIBLE);
                }
                info.setShowTz(true);
            }
        });
        holder.btn_gd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (info.getGdUser() == null){
                    holder.ll_gd.removeAllViews();
                }
                holder.btn_gd.setTextColor(Color.parseColor("#e91e63"));
                holder.btn_tz.setTextColor(Color.parseColor("#666666"));
                holder.ll_gd.setVisibility(View.VISIBLE);
                holder.type_tv.setVisibility(View.GONE);
                holder.ll_tz.setVisibility(View.GONE);
                info.setShowTz(false);
                if (info.getGdUser() == null) {
                    if (mGetGdUserListen != null) {
                        mGetGdUserListen.getGdUser(info.getRecommendId());
                    }
                }
            }
        });
        return convertView;
    }
    private void setGdUserContent(LinearLayout ll_gd, List<MyRecommendOrderUserBean> gdUser) {
        ll_gd.removeAllViews();
        for (int i = 0; i < gdUser.size() + 1; i++) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.view_myrr_gd_user, null);
            RelativeLayout center_rl = (RelativeLayout) itemView.findViewById(R.id.myrr_content_rl);
            TextView userName = (TextView) itemView.findViewById(R.id.myrr_gd_user);
            TextView amount = (TextView) itemView.findViewById(R.id.myrr_gd_amount);
            TextView bonus = (TextView) itemView.findViewById(R.id.myrr_gd_bonus);
            TextView yongJ = (TextView) itemView.findViewById(R.id.myrr_gd_yongjin);
            TextView line_tv = (TextView) itemView.findViewById(R.id.myrr_content_line_tv);

            if (i == 0) {
                center_rl.setBackgroundColor(context.getResources().getColor(R.color.color_f5f5f5));
            } else {
                MyRecommendOrderUserBean userBean = gdUser.get(i - 1);
                center_rl.setBackgroundColor(context.getResources().getColor(R.color.white));

                userName.setText(userBean.getUserName());
                amount.setText(PublicMethod.formatMoney(userBean.getFollowAmount() * 1.0));
                bonus.setText(PublicMethod.formatMoney(userBean.getWinAmount() * 1.0));
                yongJ.setText(PublicMethod.formatMoney(userBean.getShareBonusAmount() * 1.0));
            }

            if (i == gdUser.size()) {
                line_tv.setVisibility(View.GONE);
            } else {
                line_tv.setVisibility(View.VISIBLE);
            }
            ll_gd.addView(itemView);
        }
    }
    private void setContent(LinearLayout content_ll, List<MyRecommendOrderBean.DetailBean.RaceBetcodeBean> raceInfos) {
        content_ll.removeAllViews();
        for (int i = 0; i < raceInfos.size()+1; i++) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.view_myrr_content, null);
            RelativeLayout center_rl = (RelativeLayout) itemView.findViewById(R.id.myrr_content_rl);
            TextView matchId_tv = (TextView) itemView.findViewById(R.id.myrr_content_matchid);
            TextView homeAndAway_tv = (TextView) itemView.findViewById(R.id.myrr_content_home_and_away);
            TextView betCode_tv = (TextView) itemView.findViewById(R.id.myrr_content_betcode);
            TextView line_tv = (TextView) itemView.findViewById(R.id.myrr_content_line_tv);

            if (i == 0) {
                center_rl.setBackgroundColor(context.getResources().getColor(R.color.color_f5f5f5));

            } else {
                MyRecommendOrderBean.DetailBean.RaceBetcodeBean raceInfo = raceInfos.get(i-1);
                center_rl.setBackgroundColor(context.getResources().getColor(R.color.white));
                matchId_tv.setText(raceInfo.getOfficialWeekDay()+raceInfo.getMatchNum());
                homeAndAway_tv.setText(raceInfo.getHomeTeam()+" vs "+raceInfo.getAwayTeam());
                String betCodes = "";   // 投注内容
                String results = "";    // 比赛结果
                for (int j = 0; j < raceInfo.getContents().size(); j++) {
                    betCodes += raceInfo.getContents().get(j).getBetcode()+" ";
                }
                betCode_tv.setText(Html.fromHtml(betCodes, null, null));

            }

            if (i == raceInfos.size()) {
                line_tv.setVisibility(View.GONE);
            } else {
                line_tv.setVisibility(View.VISIBLE);
            }
            content_ll.addView(itemView);
        }
    }

    public void setInfos(List<MyRecommendOrderBean> infos, int type) {
        this.infos = infos;
        this.type = type;
        this.notifyDataSetChanged();
    }

    class ViewHolder {
        LinearLayout content_ll;   // 比赛信息
        ImageView prizeState_iv;   // 中奖情况（盖章图标）
        TextView time_tv;           // 发单时间
        TextView endTime_tv;        // 截止时间
        TextView amount_tv;         // 跟单彩金（订单彩金）
        TextView nChuan1_tv;        // 串关方式
        TextView type_tv;           // 保密方式
        Button btn_tz;              //投注详情
        Button btn_gd;              //跟单用户
        LinearLayout ll_tz;         //投注详情列表
        LinearLayout ll_gd;         //跟单用户列表
        LinearLayout ll_bt;
    }

    public interface GetGdUserListen {
        void getGdUser(String recommendId);
    }

    private HisCenterAdapter.GetGdUserListen mGetGdUserListen;

    public void setGdUserListerner(HisCenterAdapter.GetGdUserListen gdUserListerner) {
        this.mGetGdUserListen = gdUserListerner;
    }
}
